package common

import (
	"api/auth"
	"crypto/tls"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
)

func OutputJson(dataStr string) {
	var data interface{}
	if err := json.Unmarshal([]byte(dataStr), &data); err != nil {
		panic(err)
	}

	// 格式化输出JSON（缩进4个空格）
	formatted, err := json.MarshalIndent(data, "", "    ")
	if err != nil {
		panic(err)
	}

	fmt.Println(string(formatted))
}

func Item(target string) {
	url := "https://api.saltyun.com:30888/" + target
	method := "GET"

	// ✅ 创建跳过 TLS 验证的 http.Client
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}

	client := &http.Client{Transport: tr}
	req, err := http.NewRequest(method, url, nil)
	if err != nil {
		fmt.Println(err)
		return
	}

	token, err := auth.GetToken("sysadmin", "N48XSRmcsP5YMcPf")
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Add("X-Auth-Token", token)

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer res.Body.Close()

	body, err := io.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	OutputJson(string(body))
}
